package dataImport.action;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import dataImport.action.abstracts.AbstractEntityReader;
import dataImport.exception.MissingAttributeException;
import dataImport.model.State;
import dataImport.model.abstracts.AbstractEntity;


public class StateReader extends AbstractEntityReader {

	private static final long serialVersionUID = -3217935367707244348L;

	public static final String ENTITY_NAME = "state";

	private static final String ATTRIBUTE_NAME = "name";

	@Override
	protected AbstractEntity createEntity(final Element element) {
		final String id = element.getAttribute(ATTRIBUTE_ID);
		final String name = element.getAttribute(ATTRIBUTE_NAME);

		this.verifyRestrictions(id, name);

		return new State(id, name);
	}

	@Override
	public void read(final Document document) {
		this.read(StateReader.ENTITY_NAME, document);
	}

	protected final void verifyRestrictions(final String id, final String name) {
		super.verifyRestrictions(id);

		if (name.isEmpty()) {
			throw new MissingAttributeException(ATTRIBUTE_NAME);
		}
	}
}
